const express = require("express")
const http = require("http")
const { exec } = require("child_process")
const app = express()

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "Content-Type")
  next()
})

app.get("/api/home", (req, res) => {
  res.send("请求成功")
  exec(`cd ${__dirname} && node -v`, (err, stdout, stderr) => {
    if (err) {
      console.error(`执行出错: ${err}`)
      return
    }
    if (stderr) {
      console.error(`脚本错误输出: ${stderr}`)
      return
    }
    console.log(`脚本输出: ${stdout}`)
  })
})

const server = http.createServer((req, res) => {
  res.setHeader("content-type", "text/html;charset=utf-8")
  const url = req.url.toLowerCase()
  console.log(url, "url")
  if (url === "/api/update") {
    res.end("请求成功")
  }
})

// app.listen("8081", () => {
//   console.log("8081端口")
// })

server.listen(12138, () => {
  console.log("12138端口")
})
